<?php

declare(strict_types=1);
/**
 * Author: Zane
 * Email: 873934580@qq.com
 * Date: 2023/5/29.
 */

namespace App\Http\Request\System;

use App\Enum\MenuTypeEnum;
use App\Models\Permission;
use Illuminate\Validation\Rule;

class PermissionRequest
{
    public function rules(): array
    {
        $id = (int) request()->route('id');
        $parentId = (int) request()->get('parent_id');

        return [
            'parent_id' => $parentId ? ['integer', Rule::exists(Permission::class, 'id')] : [],
            'name' => ['required', 'max:255', Rule::unique(Permission::class)->ignore($id)],
            'title' => ['required', 'max:255', Rule::unique(Permission::class)->ignore($id)],
            'route' => [
                'required_if:type,nav',
                'max:255',
                Rule::unique(Permission::class)->whereNotNull('route')->ignore($id),
            ],
            'component' => ['required_if:type,nav', 'max:255'],
            'icon' => 'max:50',
            'weight' => 'integer|min:0|max:255',
            'note' => 'max:500',
            'type' => ['required', Rule::in(MenuTypeEnum::getValues()->toArray())],
            'status' => 'integer|in:0,1',
            'is_nav' => 'required|integer|in:0,1',
            'controller' => 'max:255',
        ];
    }
}
